<?php
/**
 * Nweb Editor
 *
 * This script is protected by copyright. Its use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.editor.app
 * @subpackage  controller
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     1.0 $Id$
 * @link        https://nweb-editor.googlecode.com
 * @link        http://editor.nweb.pl
 */

namespace pl\nweb\editor\app\controller;

/**
 * Kontroler
 *
 * @category    source
 * @package     pl.nweb.editor.app
 * @subpackage  controller
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @version     1.0 $Id$
 */
abstract class Controller extends \Zend_Controller_Action
{
    /**
     * Class constructor
     *
     * The request and response objects should be registered with the
     * controller, as should be any additional optional arguments; these will be
     * available via {@link getRequest()}, {@link getResponse()}, and
     * {@link getInvokeArgs()}, respectively.
     *
     * When overriding the constructor, please consider this usage as a best
     * practice and ensure that each is registered appropriately; the easiest
     * way to do so is to simply call parent::__construct($request, $response,
     * $invokeArgs).
     *
     * After the request, response, and invokeArgs are set, the
     * {@link $_helper helper broker} is initialized.
     *
     * Finally, {@link init()} is called as the final action of
     * instantiation, and may be safely overridden to perform initialization
     * tasks; as a general rule, override {@link init()} instead of the
     * constructor to customize an action controller's instantiation.
     *
     * @param Zend_Controller_Request_Abstract $request
     * @param Zend_Controller_Response_Abstract $response
     * @param array $invokeArgs Any additional invocation arguments
     * @return void
     */
    public function __construct(\Zend_Controller_Request_Abstract $request, \Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
    {
        parent::__construct($request, $response, $invokeArgs);
        $this->_prepareView();
        $this->_prepareLayout();
    }

    /**
     * Przygotowanie layoutu
     *
     * @return void
     */
    protected function _prepareLayout()
    {
        $layout = $this->getHelper('layout');
        if($view = $layout->getView()) {
            $path  = dirname(__DIR__);
            $path .= DIRECTORY_SEPARATOR;
            $path .= 'view';
            $path .= DIRECTORY_SEPARATOR;
            $view->addScriptPath($path . 'layout');
        }
    }

    /**
     * Przygotowanie obiektu widoku
     *
     * @return void
     */
    protected function _prepareView ()
    {
        $viewRenderer = $this->getHelper('viewRenderer');
        $view = $viewRenderer->view;
        if($view instanceof \Zend_View_Abstract) {
            $path  = dirname(__DIR__);
            $path .= DIRECTORY_SEPARATOR;
            $path .= 'view';
            $path .= DIRECTORY_SEPARATOR;

            $helperPaths = $view->getHelperPaths();
            $filterPaths = $view->getFilterPaths();

            $view->setHelperPath($path . 'helper');
            $view->setFilterPath($path . 'filter');

            if($moduleName = $this->getRequest()->getModuleName()) {
                $dir  = $path;
                $dir .= 'modules';
                $dir .= DIRECTORY_SEPARATOR;
                $dir .= $moduleName;
                $dir .= DIRECTORY_SEPARATOR;

                if(is_dir($dir)) {
                    $view->addHelperPath($dir . 'helper');
                    $view->addFilterPath($dir . 'filter');
                    $view->addScriptPath($dir . 'scripts');
                }
            }

            foreach($helperPaths as $prefix => $paths) {
                $view->addHelperPath($paths, $prefix);
            }

            foreach($filterPaths as $prefix => $paths) {
                $view->addFilterPath($paths, $prefix);
            }
        }
        return $view;
    }

    /**
     * Zwraca tłumaczenie źródła
     *
     * @param  string $messageId źródło tłumaczenia
     * @return string
     */
    protected function _ ($messageId)
    {
        $i18n = $this->i18n();
        if(null !== $i18n && $i18n->getAdapter()->getLocale()) {
            return $i18n->_($messageId);
        }
        return $messageId;
    }

    /**
     * Zapisuje wiadomość do dziennika
     *
     * @param string $message wiadomość
     * @param integer $priority priorytet
     * @return void
     */
    protected function _log($message, $priority = \Zend_Log::INFO)
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if ($bootstrap->hasResource('Log')) {
            $bootstrap->getResource('Log')->log($message, $priority);
        }
    }

    /**
     * Zwraca obiekt internacjonalizacji
     *
     * @return \Zend_Translate|null
     */
    public function i18n ()
    {
        return \pl\nweb\editor\translate\Manager::getTranslate();
    }

    /**
     * Zwraca obiekt lokalizacji
     *
     * @return \Zend_Locale
     */
    public function l10n ()
    {
        return \pl\nweb\editor\locale\Manager::getLocale();
    }
}